package com.qidi.ssh.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.qidi.ssh.bean.Customer;
import com.qidi.ssh.bean.SaleVisit;
import com.qidi.ssh.bean.User;
import com.qidi.ssh.service.CustomerService;
import com.qidi.ssh.service.SaleVisitService;
import com.qidi.ssh.service.UserService;
import com.qidi.ssh.utils.PageBean;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import java.util.Date;
import java.util.List;

/**
 * @author zhouXinBin
 * @create 2020-10-21-10:59
 */
public class SaleVisitAction extends ActionSupport implements ModelDriven<SaleVisit> {
    private UserService userService;
    private CustomerService customerService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    private SaleVisit saleVisit=new SaleVisit();

    @Override
    public SaleVisit getModel() {
        return saleVisit;
    }
    private SaleVisitService saleVisitService;

    public void setSaleVisitService(SaleVisitService saleVisitService) {
        this.saleVisitService = saleVisitService;
    }

    private Integer pageNo=1;
    private Integer pageSize=3;


    public void setPageNo(Integer pageNo) {
        if (pageNo==null){
            pageNo=1;
        }
        this.pageNo = pageNo;
    }


    public void setPageSize(Integer pageSize) {
        if (pageSize==null){
            pageSize=3;
        }
        this.pageSize = pageSize;
    }
    private Date visit_time_start;
    private Date visit_time_end;

    public void setVisit_time_start(Date visit_time_start) {
        this.visit_time_start = visit_time_start;
    }

    public void setVisit_time_end(Date visit_time_end) {
        this.visit_time_end = visit_time_end;
    }

    public String list(){
        System.out.println(visit_time_start);
        System.out.println(visit_time_end);
        DetachedCriteria detachedCriteria=DetachedCriteria.forClass(SaleVisit.class);
//        if (visit_time_start!=null && visit_time_end!=null){
//            detachedCriteria.add(Restrictions.between("visit_time",visit_time_start,visit_time_end));
//        }else if (visit_time_start!=null &&visit_time_start==null){
//            detachedCriteria.add(Restrictions.ge("visit_time",visit_time_start));
//        }else if (visit_time_start==null && visit_time_end!=null){
//            detachedCriteria.add(Restrictions.le("visit_time",visit_time_end));
//        }
        PageBean<SaleVisit> pageBean=saleVisitService.listByPage(detachedCriteria,pageNo,pageSize);
        ActionContext.getContext().getValueStack().push(pageBean);
        return "list";
    }

    public String addpage(){
        return "addpage";
    }

    public String add(){
        System.out.println(saleVisit);
        saleVisitService.save(saleVisit);
        return NONE;
    }
    public String delete(){
        saleVisitService.delete(saleVisit.getVisit_id());
        return "delete";
    }
    public String edit(){
        List<Customer> list = customerService.findAlls();
        ActionContext.getContext().getValueStack().set("list",list);
        List<User> list1 = userService.findAllUser();
        ActionContext.getContext().getValueStack().set("list1",list1);
        SaleVisit saleVisit = saleVisitService.getVisitById(this.saleVisit.getVisit_id());
        System.out.println(saleVisit);
        ActionContext.getContext().getValueStack().push(saleVisit);
        return "edit";
    }
}
